using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;


namespace Ajax.Logging
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService()]
    public class ExceptionLogService : WebService
    {
        [WebMethod()]
        public bool Log(ExceptionInfo[] exceptions)
        {
            if ((exceptions != null) && (exceptions.Length > 0))
            {
                string userName = Context.User.Identity.Name;
                bool isAuthenticated = Context.User.Identity.IsAuthenticated;
                HttpRequest request = Context.Request;

                string ipAddress = request.UserHostAddress;
                string dns = request.UserHostName;
                string userAgent = request.UserAgent;

                DateTime now = DateTime.UtcNow;

                Dictionary<string, string> cookies = new Dictionary<string, string>();

                foreach (HttpCookie cookie in request.Cookies)
                {
                    cookies.Add(cookie.Name, cookie.Value);
                }

                foreach (ExceptionInfo exception in exceptions)
                {
                    exception.IsAuthenticated = isAuthenticated;
                    exception.UserName = userName;
                    exception.IPAddress = ipAddress;
                    exception.Dns = dns;
                    exception.UserAgent = userAgent;
                    exception.ServerTime = now;
                    exception.Cookies = cookies;

                    ExceptionLogManager.Log(exception);
                }

                return true;
            }

            return false;
        }
    }
}